{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "### list v.s numpy v.s pytorch-tensor v.s pytorch-variable\n",
    "#\n",
    "#   --------> numpy\n",
    "#   |           ^\n",
    "# list          |         \n",
    "#   |           V               \n",
    "#   -----> pytorch-tensor <---> pytorch-variable\n",
    "#========================================\n",
    "import torch\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "list1 = [[3,2,1],[4,5,6]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[3, 2, 1], [4, 5, 6]]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[3 2 1]\n",
      " [4 5 6]]\n",
      "\n",
      " 3  2  1\n",
      " 4  5  6\n",
      "[torch.FloatTensor of size 2x3]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#========================================\n",
    "### list -> numpy\n",
    "###      -> pytorch-tensor\n",
    "#----------------------------------------\n",
    "# list > numpy:\n",
    "arr1 = np.array(list1)\n",
    "print(arr1)\n",
    "# list > pytorch-tensor\n",
    "tr1 = torch.FloatTensor(list1)\n",
    "print(tr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " 3  2  1\n",
      " 4  5  6\n",
      "[torch.LongTensor of size 2x3]\n",
      "\n",
      "[[ 3.  2.  1.]\n",
      " [ 4.  5.  6.]]\n"
     ]
    }
   ],
   "source": [
    "#----------------------------------------\n",
    "# numpy > pytorch-tensor\n",
    "tr2 = torch.from_numpy(arr1) # dtype same as numpy\n",
    "print(tr2)\n",
    "# pytorch-tensor > numpy\n",
    "arr2 = tr1.numpy()\n",
    "print(arr2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variable containing:\n",
      " 3  2  1\n",
      " 4  5  6\n",
      "[torch.FloatTensor of size 2x3]\n",
      "\n",
      "Variable containing:\n",
      " 15.1667\n",
      "[torch.FloatTensor of size 1]\n",
      "\n",
      "Variable containing:\n",
      " 1.0000  0.6667  0.3333\n",
      " 1.3333  1.6667  2.0000\n",
      "[torch.FloatTensor of size 2x3]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#----------------------------------------\n",
    "# pytorch-tensor > pytorch-variable\n",
    "from torch.autograd import Variable\n",
    "\n",
    "var1 = Variable(tr1, requires_grad=True) # only variable can realize gradient, update.\n",
    "print(var1)                              # e.g. weights\n",
    "\n",
    "v_out = torch.mean(var1*var1) # var1^2\n",
    "print(v_out)\n",
    "\n",
    "v_out.backward()\n",
    "print(var1.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      " 3  2  1\n",
      " 4  5  6\n",
      "[torch.FloatTensor of size 2x3]\n",
      "\n",
      "[[ 3.  2.  1.]\n",
      " [ 4.  5.  6.]]\n"
     ]
    }
   ],
   "source": [
    "# pytorch-variable > pytorch-tensor\n",
    "print(var1.data)\n",
    "# pytorch-tensor > numpy\n",
    "print(var1.data.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
